﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chefbook.HelperMethods;
using System.Collections.ObjectModel;

namespace Chefbook.DataAccessLayer
{
    public partial class Recipe
    {
        public Recipe()
        {
            _Description = string.Empty;
            _Directions = string.Empty;
            _Name = string.Empty;
        }
        /// <summary>
        /// Moves a recipe from one category to another
        /// </summary>
        /// <param name="from">The originating category</param>
        /// <param name="to">The category to move to</param>
        public void ChangeCategory(Category from, Category to)
        {
            from.ThrowIfNull("from");
            to.ThrowIfNull("to");

            to.AddRecipe(this);
            from.RemoveRecipe(this);
        }

        /// <summary>
        /// Gets a readonlylist of categories this recipe belongs to
        /// </summary>
        public ReadOnlyCollection<Ingredient> Ingredients
        {
            get { return ingredients.ToList().AsReadOnly(); }
        }

        /// <summary>
        /// Gets a readonlylist of categories this recipe belongs to
        /// </summary>
        public ReadOnlyCollection<Category> Categories
        {
            get { return categories.ToList().AsReadOnly(); }
        }

        public void AddIngredient(Ingredient ingredientToAdd)
        {
            this.ingredients.Add(ingredientToAdd);
        }

        /// <summary>
        /// Removes an ingredient from the recipe
        /// </summary>
        /// <param name="ingredientToRemove">The ingredient to remove</param>
        public void RemoveIngredient(Ingredient ingredientToRemove)
        {
            ingredientToRemove.ThrowIfNull("ingredientToRemove");
            ParameterValidation.ValidateArgument(() => ingredients.Contains(ingredientToRemove), "ingredientToRemove");
            ingredients.Remove(ingredientToRemove);
        }

        /// <summary>
        /// Removes All links for this Recipe
        /// </summary>
        public void Delete()
        {
            var tempCategories = new List<Category>(Categories);
            foreach (var category in tempCategories)
            {
                categories.Remove(category);
            }
            var tempIngredients = new List<Ingredient>(ingredients);
            foreach (var ingredient in tempIngredients)
            {
                ingredients.Remove(ingredient);
            }
        }

        /// <summary>
        /// Gets or sets the Cook Time.  Note seconds are ignored
        /// </summary>
        public TimeSpan CookTime
        {
            get { return TimeSpan.FromMinutes(CookTimeTotalMin); }
            set { CookTimeTotalMin = (int)value.TotalMinutes; }
        }

        /// <summary>
        /// Gets or sets the Cook Time.  Note seconds are ignored
        /// </summary>
        public TimeSpan PrepTime
        {
            get { return TimeSpan.FromMinutes(PrepTimeTotalMin); }
            set { PrepTimeTotalMin = (int)value.TotalMinutes; }
        }
    }
}
